package com.lyshop.jinxiang.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.application.MyApplication;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.bean.FollowListBean;
import com.lyshop.jinxiang.bean.FollowUserBean;
import com.lyshop.jinxiang.core.AppAction;
import com.lyshop.jinxiang.net.ActionCallbackListener;
import com.lyshop.jinxiang.utils.GlideUtils;
import com.lyshop.jinxiang.utils.RouteUtil;
import com.lyshop.jinxiang.utils.ToastUtil;
import com.lyshop.jinxiang.view.PromptDialog;

/**
 * Created by Mr on 2018/9/13.
 */

public class FollowListAdapter extends CommonAdapter<FollowListBean> {
    private AppAction mAppAction;
    private FollowListListener mListener;
    private boolean isShowAdd;

    public FollowListAdapter(Context context, boolean isShowAdd, FollowListListener listener) {
        super(context);
        this.mListener = listener;
        this.isShowAdd = isShowAdd;
        this.mAppAction = ((MyApplication) context.getApplicationContext()).getAppAction();
    }

    public void removeItem(FollowListBean bean) {
        mDatas.remove(bean);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_follow_list, parent, false);
            viewHolder = new ViewHolder(convertView);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.setData(position);
        return convertView;
    }

    class ViewHolder {
        private View view_user_info;
        private ImageView iv_header;
        private TextView tv_user_name;
        private TextView tv_follow;

        ViewHolder(View view) {
            view.setTag(ViewHolder.this);
            view_user_info = view.findViewById(R.id.view_user_info);
            iv_header = view.findViewById(R.id.iv_header);
            tv_user_name = view.findViewById(R.id.tv_user_name);
            tv_follow = view.findViewById(R.id.tv_follow);

        }

        void setData(int position) {
            try {
                final FollowListBean mUserBean = mDatas.get(position);
                GlideUtils.showImageOptions(mContext, mUserBean.header_img, iv_header);
                tv_user_name.setText(mUserBean.nickname);

                if (isShowAdd) {
                    tv_follow.setVisibility(View.VISIBLE);
                } else {
                    tv_follow.setVisibility(View.GONE);
                }

                if (mUserBean.is_follow) {
                    tv_follow.setText("已关注");
                    tv_follow.setTextColor(ContextCompat.getColor(mContext, R.color.text_gray));
                } else {
                    tv_follow.setText("+关注");
                    tv_follow.setTextColor(ContextCompat.getColor(mContext, R.color.lt_blue));
                }

                tv_follow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mUserBean.is_follow) {
                            new PromptDialog.Builder(mContext)
                                    .setTitle("确定不再关注此人？")
                                    .setButton("取消", "确定")
                                    .setOnConfirmListener(new PromptDialog.OnConfirmListener() {
                                        @Override
                                        public void onConfirm() {
                                            follow(mUserBean.is_follow, mUserBean.user_id, mUserBean);
                                        }
                                    }).show();
                        } else {
                            follow(!mUserBean.is_follow, mUserBean.user_id, mUserBean);
                        }
                    }
                });
                view_user_info.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RouteUtil.toFollowOrderUserActivity((BaseActivity) mContext, mUserBean.user_id);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private void follow(final boolean isAdd, final String fid, final FollowListBean mUserBean) {
            FollowUserBean bean = new FollowUserBean();
            bean.userId = fid;
            bean.type = isAdd ? 2 : 1;

            mAppAction.followUser(bean, new ActionCallbackListener<Void>() {
                @Override
                public void onSuccess(Void data) {
                    mListener.onCancelLoading();
                    mListener.onFollowChanged(isAdd, mUserBean);
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    ToastUtil.showToast(mContext, message);
                    mListener.onCancelLoading();
                }
            });

        }
    }

    public interface FollowListListener {
        void onLoading();

        void onCancelLoading();

        void onLogout();

        void onFollowChanged(boolean isAdd, FollowListBean bean);
    }
}
